package cn.itrip.beans.vo.hotel;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;

import java.io.IOException;
import java.util.List;

public class HotelTest {

    public static String url="http://localhost:8080/solr/hotel";

    public static void main(String[] args) {
        //1.创建HttpSolrClient对象
        HttpSolrClient httpSolrClient = new HttpSolrClient(url);

        //配置解析器
        httpSolrClient.setParser(new XMLResponseParser());
        httpSolrClient.setConnectionTimeout(500);

        //2.设置查询参数 solrQuery
        SolrQuery query = new SolrQuery("*:*");//设置Q的查询参数

        //设置FQ
        //query.addFilterQuery();
        query.setSort("id",SolrQuery.ORDER.asc);
        query.setStart(0);
        query.setRows(5);

        //3.接收返回数据，转化成为对象 queryResponse
        QueryResponse response = null;
        List<Hotel> hotelList = null;
        try{
            response = httpSolrClient.query(query);
            hotelList = response.getBeans(Hotel.class);
            for (Hotel hotel:hotelList) {
                System.out.println(hotel.getHotelName()+">>>");
            }
        } catch (SolrServerException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
